package com.eedi.framework.sms.controller.sys;

import com.aliyuncs.utils.StringUtils;
import com.eedi.framework.common.exception.ServerException;
import com.eedi.framework.common.pojo.CommonResult;
import com.eedi.framework.common.pojo.PageResult;
import com.eedi.framework.common.validation.UpdateGroup;
import com.eedi.framework.sms.controller.sys.vo.SysSmsPageReq;
import com.eedi.framework.sms.controller.sys.vo.SysSmsUpdateStatusReq;
import com.eedi.framework.sms.enums.SysSmsErrorCodeConstants;
import com.eedi.framework.sms.controller.sys.vo.SysSmsResp;
import com.eedi.framework.sms.controller.sys.vo.SysSmsSaveReq;
import com.eedi.framework.sms.service.SmsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.validation.Valid;

import static com.eedi.framework.common.pojo.CommonResult.success;

@Tag(name = "系统界面 - 短信")
@RestController
@RequestMapping("/sms")
@Validated
public class SysSmsController {

    @Resource
    private SmsService smsService;

    @PostMapping("/create")
    @Operation(summary = "创建短信")
    public CommonResult<String> createSms(@Validated({UpdateGroup.class}) @RequestBody SysSmsSaveReq createReqVO) {
        return success(smsService.createSms(createReqVO));
    }

    @PostMapping("/update")
    @Operation(summary = "更新短信")
    public CommonResult<Boolean> updateSms(@Validated({UpdateGroup.class}) @RequestBody SysSmsSaveReq updateReqVO) {
        smsService.updateSms(updateReqVO);
        return success(true);
    }

    @GetMapping("/delete")
    @Operation(summary = "删除短信")
    @Parameter(name = "systemSmsId", description = "编号", required = true)
    public CommonResult<Boolean> deleteSms(String systemSmsId) {
        smsService.deleteSms(systemSmsId);
        return success(true);
    }

    @PostMapping("/update-status")
    @Operation(summary = "修改短信配置状态")
    @PermitAll
    public CommonResult<Boolean> updateSmsStatus(@Valid @RequestBody SysSmsUpdateStatusReq reqVO) {
        if (StringUtils.isEmpty(reqVO.getSystemSmsId()) && StringUtils.isEmpty(reqVO.getSystemSmsTemplateId())) {
            throw new ServerException(SysSmsErrorCodeConstants.SYS_SMS_MSG_OR_TEMPLATE_ID_PARAM_ALL_EMPTY);
        }
        smsService.updateSmsStatus(reqVO.getSystemSmsId(), reqVO.getSystemSmsTemplateId(), reqVO.getStatus());
        return success(true);
    }

    @PostMapping("/page")
    public CommonResult<PageResult<SysSmsResp>> getSmsPage(@RequestBody @Valid SysSmsPageReq pageReqVO) {
        PageResult<SysSmsResp> pageResult = smsService.getSmsPage(pageReqVO);
        return success(pageResult);
    }

}
